package com.traffic.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.traffic.annotation.ApiJsonObject;
import com.traffic.annotation.ApiJsonProperty;
import com.traffic.constant.Status;


import com.traffic.entity.Testdata;
import com.traffic.exception.BaseException;
import com.traffic.model.ApiResponse;


import com.traffic.service.TestdataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Api(tags = "小样测试相关接口")
@RestController
@RequestMapping(value = "/test")
@Slf4j
public class TestController {

    @Autowired
    private TestdataService testdataService;

    @ApiOperation(value = "【增加收藏地点】", notes = "新增地点")
    @PostMapping("/insert")
    @ApiJsonObject(name = "collect",value = {
            @ApiJsonProperty(type =String.class,key = "timestap",example = "" , description = "时间戳"),
            @ApiJsonProperty(type =Double.class,key = "course",example = "60.5" , description = "方向角"),

            @ApiJsonProperty(type =Double.class,key = "speed",example = "60.5" , description = "速度"),
            @ApiJsonProperty(type =Double.class,key = "longitude",example = "118.23456" , description = "经度"),
            @ApiJsonProperty(type =Double.class,key = "latitude",example = "30.34567" , description = "纬度"),
    })
    public ApiResponse insertCollection(@RequestBody Map<String, Object> collect) {

        String param[] = new String[]{"timestap","speed","course","longitude","latitude"};

        List<String> collList = CollUtil.newArrayList(param);
        if (!collList.containsAll(CollUtil.newArrayList(collect.keySet().toArray()))) {
            throw new BaseException(Status.PARAM_ERROR);
        }

        Testdata col = new Testdata();
        BeanUtil.copyProperties(collect, col, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        testdataService.save(col);

        return ApiResponse.ofStatus(Status.OK);
    }

}
